import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/frontHome",
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/regist",
    name: "Regist",
    component: () => import("../views/Regist.vue"),
  },

  {
    path: "/backHome",
    name: "BackHome",
    component: () => import("../views/back/BackHome.vue"),
    children: [
      {
        path: "",
        name: "Index",
        component: () => import("../views/back/Index.vue"),
      },
      {
        path: "userMange",
        name: "UserManage",
        component: () => import("../views/back/UserManage.vue"),
      },
      {
        path: "AccountMange",
        name: "AccountManage",
        component: () => import("../views/back/AccountManage.vue"),
      },
      {
        path: "AnnouncementManage",
        name: "AnnouncementManage",
        component: () => import("../views/back/AnnouncementManage.vue"),
      },

      {
        path: "userInfo",
        name: "UserInfo",
        component: () => import("../views/back/UserInfo.vue"),
        children: [
          {
            path: "",
            name: "UserProfile",
            component: () => import("../views/back/UserProfile.vue"),
          },
          {
            path: "userPwd",
            name: "UserPwd",
            component: () => import("../views/back/UserPwd.vue"),
          },
        ],
      },
      {
        path: "goodsMange",
        name: "GoodsMange",
        component: () => import("../views/back/GoodsMange.vue"),
      },
      {
        path: "CollectionRightsManage",
        name: "CollectionRightsManage",
        component: () => import("../views/back/CollectionRightsManage.vue"),
      },

      {
        path: "OrderManage",
        name: "OrderManage",
        component: () => import("../views/back/OrderManage.vue"),
      },

      {
        path: "CustomerServiceManage",
        name: "CustomerServiceManage",
        component: () => import("../views/back/CustomerServiceManage.vue"),
      },
    ],
  },
  {
    path: "/frontHome",
    name: "FrontHome",
    component: () => import("../views/front/FrontHome.vue"),
    children: [
      {
        path: "",
        name: "Index",
        component: () => import("../views/front/Index.vue"),
      },
      {
        path: "IncreaseMarket",
        name: "IncreaseMarket",
        component: () => import("../views/front/IncreaseMarket.vue"),
      },
      {
        path: "ShoppingCart",
        name: "ShoppingCart",
        component: () => import("../views/front/ShoppingCart.vue"),
      },
      {
        path: "OrderManagement",
        name: "OrderManagement",
        component: () => import("../views/front/OrderManagement.vue"),
      },
      {
        path: "CustomerManagement",
        name: "CustomerManagement",
        component: () => import("../views/front/CustomerManagement.vue"),
      },
      {
        path: "WarehouseManagement",
        name: "WarehouseManagement",
        component: () => import("../views/front/WarehouseManagement.vue"),
      },
      {
        path: "CoolectionInfo",
        name: "CoolectionInfo",
        component: () => import("../views/front/CoolectionInfo.vue"),
      },
      {
        path: "userInfo",
        name: "UserInfo",
        component: () => import("../views/front/UserInfo.vue"),
        children: [
          {
            path: "",
            name: "UserProfile",
            component: () => import("../views/front/UserProfile.vue"),
          },
          {
            path: "userPwd",
            name: "UserPwd",
            component: () => import("../views/front/UserPwd.vue"),
          },
        ],
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  routes,
});

// 添加请求拦截器
router.beforeEach((to, from, next) => {
  console.log(to.name);
  if (
    to.name == "Regist" ||
    to.name == "Login" ||
    to.path == "/frontHome" ||
    to.path == "/frontHome/CoolectionInfo"
  ) {
    // 以上请求放行
    next();
    return;
  }
  // 获取浏览器的用户信息
  const userAuth = sessionStorage.getItem("userAuth");
  if (userAuth == 2) {
    // root用户随便访问
    next();
    return;
  } 
  if (userAuth == 1) {
    if (
      to.path == "/backHome" ||
      to.path == "/backHome/" ||
      to.path == "/backHome/OrderManage" ||
      to.path == "/backHome/CustomerServiceManage" ||
      to.path == "/backHome/userMange" ||
      to.path == "/backHome/AccountMange" ||
      to.path == "/backHome/AnnouncementManage" ||
      to.path == "/backHome/goodsMange" ||
      to.path == "/backHome/CollectionRightsManage/"
    ) {
      alert("用户权限不够无法访问,请输入对应的账号访问");
      next(from.path);
      return;
    }
    next();
    return;
  }
  if (userAuth == null) {
  // alert("请登录")
  // 否则跳转登录页面
  next("Login");
  return;
  }
});

// 解决 vue-router 升级导致的 Uncaught(in promise)
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err);
};
export default router;
